package handler

// func newProxyHandler() {

// }

// package handler

import (
	"io"
	bilivod "iptv_server/v3/v2_src/bili_vod"
	"iptv_server/v3/v2_src/handler/middleware"
	"iptv_server/v3/v2_src/utils"
	"net/http"
	"strings"
)

const (
	onlineVodApi string = "/api/onlinevod/"
)

func initOnlineVod(mux *http.ServeMux, middles ...middleware.MiddleWare) {
	h := middleware.ApplyMiddles(&onlineVod{}, middles...)

	// parsers = make(map[string]Parser)

	// parsers["m3u"]=&

	mux.Handle(onlineVodApi, h)
}

type onlineVod struct {
}

// "/api/onlinevod/from_bili/aid/cid"
// "0:/1:api/2:onlinevod/3:from_bili/4:aid/5:cid"
func (o *onlineVod) ServeHTTP(w http.ResponseWriter, r *http.Request) {

	ps := strings.Split(r.URL.Path, "/")
	if len(ps) < 4 {
		// do something
		return
	}

	// get from cache

	site := strings.ToLower(ps[3])
	utils.Logger.Debugf("onlinevod : site=%s", site)
	if site == bilivod.FROM_BILIBILI {
		if len(ps) == 4 {

		} else {
			//>4
			aid := ps[4]
			cid := ""
			if len(ps) > 5 {
				cid = ps[5]
			}
			utils.Logger.Debugf("%s, aid=%s, cid=%s", bilivod.FROM_BILIBILI, aid, cid)
			data := bilivod.GetData(aid, cid, onlineVodApi)
			if len(data) > 0 {
				w.WriteHeader(http.StatusOK)
				io.WriteString(w, data)
			}
		}
	}
}

// curl http://localhost:9966/api/onlinevod/from_bili/683317337
// curl http://localhost:9966/api/onlinevod/from_bili/season_id_33415

// curl http://localhost:9966/api/onlinevod/from_bili/season_id_33415/eid_cid
// curl http://localhost:9966/api/onlinevod/from_bili/season_id_33415/323085_184593713

// curl http://localhost:9966/api/onlinevod/from_bili/BV19f4y1a7DZ/get_bili_aid
